package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import news.Comments;
import news.Tool;

public class CommentsDAO extends Tool {
	public static CommentsDAO DAO = new CommentsDAO();

	public static List<Comments> showComments(int nid) {// 获取评论
		List<Comments> list = new ArrayList<Comments>();
		Comments s = null;
		Connection con = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		String sql = "select * from comments where cnid=? ";
		try {
			con = Tool.open();
			st = con.prepareStatement(sql);
			st.setInt(1, nid);
			rs = st.executeQuery();
			while (rs.next()) {
				s = new Comments();
				s.setCid(rs.getInt("cid"));
				s.setCnid(rs.getInt("cnid"));
				s.setCauthor(rs.getString("cauthor"));
				s.setCdate(rs.getTimestamp("cdate"));
				s.setConntent(rs.getString("ccontent"));
				s.setCip(rs.getString("cip"));
				list.add(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);

		} finally {
			Tool.close(st, con, rs);
		}
		return list;
	}

	public int insert(Comments c) {// 新增评论
		String sql = "insert into comments(cnid,ccontent,cdate,cip,cauthor) values(?,?,?,?,?)";
		int i = super.executeUpdate(sql, c.getCnid(), c.getConntent(),
				c.getCdate(), c.getCip(), c.getCauthor());
		return i;
	}
}
